using UnityEngine;
using UnityEngine.UIElements;
using MinesweeperPlus.Scripts.Abstractions.Enums;
using MinesweeperPlus.Scripts.Configs;
using MinesweeperPlus.Scripts.Utils;
using System;

namespace MinesweeperPlus.Scripts.UI
{
    public sealed class PlayfieldPanel : UIElement
    {
        public new class UxmlFactory : UxmlFactory<PlayfieldPanel> { }

        #region Constants
        private const int NUMBER_OF_STARS = 3;
        private const string WIN_TEXT = "Win!";
        private const string LOSE_TEXT = "Lose!";
        #endregion

        #region Actions
        public Action OnBackButtonClick;
        public Action OnVideoButtonClick;
        public Action OnRestartButtonClick;
        #endregion

        #region Variables
        private Label _label;
        private IMGUIContainer[] _starsIcons;
        private VisualElement _buttonsPanel;
        private Button _backButton;
        private IMGUIContainer _backIcon;
        private Button _videoButton;
        private IMGUIContainer _videoIcon;
        private Button _restartButton;
        private IMGUIContainer _restartIcon;
        #endregion

        #region PlayfieldPanel Methods
        public PlayfieldPanel() : base() { }
        public PlayfieldPanel(IconsConfig iconsConfig, StyleSheet styleSheet) : 
            base(iconsConfig, styleSheet) { }

        public void ShowVideoButton()
        {
            _videoButton.SetDisplay(true);
        }
        public void HideVideoButton()
        {
            _videoButton.SetDisplay(false);
        }

        public void ShowWinPanel(int numberOfStars)
        {
            ShowEndPanel(WIN_TEXT);
            foreach (var starIcon in _starsIcons)
                starIcon.SetBackgroundImage(
                    numberOfStars-- > 0 ? IconsConfig.StarIcon : IconsConfig.EmptyStarIcon);

        }
        public void ShowLosePanel()
        {
            ShowEndPanel(LOSE_TEXT);
            foreach (var starIcon in _starsIcons)
                starIcon.SetBackgroundImage(IconsConfig.EmptyStarIcon);
        }
        public void HideEndPanel()
        {
            Panel.SetDisplay(false);
        }
        #endregion

        #region Other Methods
        protected override void UpdateColorScheme()
        {
            base.UpdateColorScheme();

            Panel.SetBackgroundColor(ColorSchemeConfig.BackgroundColor);
            Panel.SetBorderColor(ColorSchemeConfig.BorderColor);

            _label.SetTextColor(ColorSchemeConfig.TextColor);

            foreach (var icon in _starsIcons)
                icon.SetBackgroundImageColor(ColorSchemeConfig.IconColor);

            _backButton.SetBorderColor(ColorSchemeConfig.BorderColor);
            _backIcon.SetBackgroundImageColor(ColorSchemeConfig.IconColor);

            _videoButton.SetBorderColor(ColorSchemeConfig.BorderColor);
            _videoIcon.SetBackgroundImageColor(ColorSchemeConfig.IconColor);

            _restartButton.SetBorderColor(ColorSchemeConfig.BorderColor);
            _restartIcon.SetBackgroundImageColor(ColorSchemeConfig.IconColor);
        }

        protected override void CreatePanel()
        {
            base.CreatePanel();
            Panel.AddToClassList(Constants.UI_PLAYFIELD_PANEL_STYLE);

            _label = new Label("Text");
            Panel.Add(_label);

            var starsPanel = CreateVisualElement(Constants.UI_PLAYFIELD_STARS_PANEL_STYLE);
            _starsIcons = new IMGUIContainer[NUMBER_OF_STARS];
            for (int i = 0; i < NUMBER_OF_STARS; i++)
            {
                var starIcon = CreateIMGUIContainer(IconsConfig.EmptyStarIcon);
                starsPanel.Add(starIcon);
                _starsIcons[i] = starIcon;
            }

            _buttonsPanel = CreateVisualElement(Constants.UI_PLAYFIELD_BUTTONS_PANEL_STYLE);

            _backIcon = (IMGUIContainer)CreateIMGUIContainer(IconsConfig.NextIcon).Rotate(180);
            _backButton = this.CreateButton(_backIcon, () => 
                {
                    OnBackButtonClick?.Invoke();
                    HideEndPanel();
                });

            _videoIcon = CreateIMGUIContainer(IconsConfig.VideoIcon);
            _videoButton = this.CreateButton(_videoIcon, () => OnVideoButtonClick?.Invoke());

            _restartIcon = CreateIMGUIContainer(IconsConfig.RestartIcon); 
            _restartButton = this.CreateButton(_restartIcon, () => 
                {
                    OnRestartButtonClick?.Invoke();
                    HideEndPanel();
                });

            HideEndPanel();
        }

        protected override Button CreateButton(
            IMGUIContainer imguiContainer, 
            Action buttonClicked, 
            params string[] classesNames)
        {
            var button = base.CreateButton(imguiContainer, buttonClicked, classesNames);
            button.AddToClassList(Constants.UI_PLAYFIELD_BUTTON_STYLE);
            _buttonsPanel.Add(button);
            return button;
        }
        private VisualElement CreateVisualElement(string className)
        {
            var visualElement = new VisualElement();
            visualElement.AddToClassList(className);
            Panel.Add(visualElement);
            return visualElement;
        }
        
        private void ShowEndPanel(string text)
        {
            Panel.SetDisplay(true);
            _label.text = text;
        }
        #endregion
    }
}
